package com.example.points.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.points.domain.PointsOperationLog;
import com.example.points.mapper.PointsOperationLogMapper;
import com.example.points.service.PointsOperationLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class PointsOperationLogServiceImpl implements PointsOperationLogService {
    private final PointsOperationLogMapper operationLogMapper;

    @Override
    public void recordOperation(Long userId, Integer operationType, Long points,
                              Long beforePoints, Long afterPoints, String operator, String remark) {
        PointsOperationLog log = new PointsOperationLog();
        log.setUserId(userId);
        log.setOperationType(operationType);
        log.setPoints(points);
        log.setBeforePoints(beforePoints);
        log.setAfterPoints(afterPoints);
        log.setOperator(operator);
        log.setOperationTime(LocalDateTime.now());
        log.setRemark(remark);
        operationLogMapper.insert(log);
    }

    @Override
    public IPage<PointsOperationLog> getOperationLogs(Long userId, Integer operationType,
                                                     Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<PointsOperationLog> wrapper = new LambdaQueryWrapper<>();
        if (userId != null) {
            wrapper.eq(PointsOperationLog::getUserId, userId);
        }
        if (operationType != null) {
            wrapper.eq(PointsOperationLog::getOperationType, operationType);
        }
        wrapper.orderByDesc(PointsOperationLog::getOperationTime);
        
        return operationLogMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }
} 